﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CanDoo.Extensions;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Admin.Core.Models;
using Newtonsoft.Json;
using CanDoo.Extensions.SeedExtensions;
using System.Text;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;

namespace CanDoo.Oil.WebHost
{
    public partial class Seed : CanDoo.FineUI.PageBase
    {
        public IRoleContract RoleManager { get; set; }
        public IUserContract UserManager { get; set; }
        public IModulePowerContract ModulePowerManager { get; set; }
        public IModuleContract ModuleManager { get; set; }
        public IParameterClassContract ParameterClassContract { get; set; }
        public IParameterListContract ParameterListContract { get; set; }
        public INavigationContract NavigationManager { get; set; }
        public IConfigContract ConfigContract { get; set; }
        public ISupplierContract SupplierContract { get; set; }
        public IWarehouseContract WarehouseContract { get; set; }

        public IProductContract ProductContract { get; set; }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            DB.KeepReading(MakeSeed);
        }

        private void MakeSeed()
        {
            var sb = new StringBuilder();

            sb.AppendLine("#region 种子");

            #region 基础数据

            sb.AppendLine("#region ParameterClass");

            var parameterClasses = ParameterClassContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(ParameterClass).Name + ">().AddRange(");
            sb.Append(parameterClasses.ToSeedData<ParameterClass>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region Config");
            var configLists = ConfigContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Config).Name + ">().AddRange(");
            sb.Append(configLists.ToSeedData<Config>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region role");
            var roles = RoleManager.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Role).Name + ">().AddRange(");
            sb.Append(roles.ToSeedData<Role>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region User");
            var users = UserManager.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(User).Name + ">().AddRange(");
            sb.Append(users.ToSeedData<User>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region Module");
            var models = ModuleManager.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Module).Name + ">().AddRange(");
            sb.Append(models.ToSeedData<Module>());
            sb.AppendLine(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();


            sb.AppendLine("#region ModulePower");
            var powers = ModulePowerManager.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(ModulePower).Name + ">().AddRange(");
            sb.Append(powers.ToSeedData<ModulePower>());
            sb.AppendLine(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region Navigation");
            var navigations = NavigationManager.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Navigation).Name + ">().AddRange(");
            sb.Append(navigations.ToSeedData<Navigation>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region ParameterList");
            var parameterLists = ParameterListContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(ParameterList).Name + ">().AddRange(");
            sb.Append(parameterLists.ToSeedData<ParameterList>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            #endregion

            sb.AppendLine("#region supplierList");
            var supplierList = SupplierContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Supplier).Name + ">().AddRange(");
            sb.Append(supplierList.ToSeedData<Supplier>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region warehouseList");
            var warehouseList = WarehouseContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Warehouse).Name + ">().AddRange(");
            sb.Append(warehouseList.ToSeedData<Warehouse>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#region ProductList");
            var ProductList = ProductContract.Entities.ToList();
            sb.AppendLine("context.Set<" + typeof(Product).Name + ">().AddRange(");
            sb.Append(ProductList.ToSeedData<Product>());
            sb.Append(");");
            sb.AppendLine("\r\ncontext.SaveChanges(); ");
            sb.AppendLine("\r\n#endregion ");

            sb.AppendLine();
            sb.AppendLine();

            sb.AppendLine("#endregion 种子");

            var strs = sb.ToString().Split("\r\n");
            var list = from str in strs select ("\t\t\t" + str);

            Response.Write(string.Join("\r\n",list));
            //Response.Write(models.ToJsonString<Module>());
            //Response.Write(JsonConvert.SerializeObject(models, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }));
            Response.End();
        }
    }
}